<?php

/**
 * @author   Springlee <jslichun123@gamil.com>
 * @version  2014-6-13 上午11:19:29
 */
class  Warehouse extends Basic
{

    public static function get_order($orderid)
    {
        $erp_order = DB::select()
            ->from('erp_orders')
            ->where('erp_orders_id','=',$orderid)
            ->execute('readonlyerp')
            ->current();

        return $erp_order;
    }

    public static function get_product($orderid)
    {
        return DB::select()
            ->from('erp_orders_products')
            ->where('erp_orders_id','=',$orderid)
            ->execute('readonlyerp')
            ->as_array();
    }

    public static function in_status($sku,$orderid)
    {
        $id = DB::select('id')
            ->from('deliveryorders')
            ->where('sku','=',$sku)
            ->and_where('order_id','=',$orderid)
            ->execute('newerp')
            ->get('id',0);
        if($id){
            return '已出库';
        }else{
            return '未出库';
        }
    }

    public static function  update_warehouse($condition)
    {
        $total_rows = DB::update('warehouseorders')
            ->set(array(
                'warehouse_quantity'     => $condition['warehouse_quantity'],
                'warehouse_date'         => $condition['warehouse_date'],
                'type'                   => $condition['type'],
                'remark'                 => $condition['remark'],
                'single_price'           => $condition['single_price'],
                'money'                  => $condition['money'],
                'cost'                   => $condition['cost'],
                'transportation_expense' => $condition['transportation_expense']
            ))
            ->where('id','=',$condition['id'])
            ->execute('newerp');
        return $total_rows;
    }

    public static function tuijian_insert($condition)
    {
        $info = DB::select('single_price','quantity','cost','sku','sku_name','sku_standard','storage_position')
            ->from('deliveryorders')
            ->where('orders_products_id','=',$condition['opid'])
            ->execute('newerp')
            ->current();

        $wid  = DB::select('id')
            ->from('waster_receipts')
            ->where('table','=','erp_orders')
            ->and_where('table_id','=',$condition['orderid'])
            ->execute('newerp')
            ->get('id',0);
        $wid2 = DB::select('id')
            ->from('warehouseorders')
            ->where('orderid','=',$condition['orderid'])
            ->execute('newerp')
            ->get('id',0);
        if($wid||$wid2){
            return '该订单已经退回入库了';
        }
        try{
            if($condition['hkl']){
                DB::insert('warehouseorders',array(
                    'orderid',
                    'warehouse_date',
                    'op_time',
                    'sku',
                    'sku_name',
                    'sku_standard',
                    'storage_position',
                    'warehouse_quantity',
                    'single_price',
                    'money',
                    'cost',
                    'type',
                    'warehouse_man'
                ))
                    ->values(array(
                        $condition['orderid'],
                        date('Y-m-d H:i:s'),
                        date('Y-m-d H:i:s'),
                        strtoupper($info['sku']),
                        $info['sku_name'],
                        $info['sku_standard'],
                        $info['storage_position'],
                        $condition['hkl'],
                        $info['single_price'],
                        $info['single_price']*$condition['hkl'],
                        $info['cost']/$info['quantity']*$condition['hkl'],
                        20,
                        User::active_user()->id
                    ))
                    ->execute('newerp');
            }
            if($condition['bfl']){
                DB::insert('waster_receipts',array(
                    'sku',
                    'sku_name',
                    'sku_standard',
                    'storage_position',
                    'quantity',
                    'single_price',
                    'money',
                    'cost',
                    'table',
                    'table_id',
                    'remark',
                    'waster_receipts_time'
                ))
                    ->values(array(
                        strtoupper($info['sku']),
                        $info['sku_name'],
                        $info['sku_standard'],
                        $info['storage_position'],
                        $condition['bfl'],
                        $info['single_price'],
                        $info['single_price']*$condition['bfl'],
                        $info['cost']/$info['quantity']*$condition['bfl'],
                        'erp_orders',
                        $condition['orderid'],
                        $condition['remark'],
                        date('Y-m-d H:i:s')
                    ))
                    ->execute('newerp');
            }
            return '保存成功';
        }catch(Exception $e){
            return '保存失败';
        }
    }


    public static function insert_warehouse($condition)
    {

        list($insert_id,) = DB::insert('warehouseorders',array(
            'warehouseid',
            'warehouse_date',
            'op_time',
            'sku',
            'sku_name',
            'sku_standard',
            'storage_position',
            'warehouse_quantity',
            'single_price',
            'money',
            'cost',
            'type',
            'warehouse_man',
            'remark',
            'supplier_id',
            'enterprise_dominant'
        ))->values(array(
                $condition['warehouseid'],
                $condition['delivery_date'],
                date('Y-m-d H:i:s'),
                strtoupper($condition['sku']),
                $condition['sku_name'],
                $condition['sku_standard'],
                $condition['storage_position'],
                $condition['quantity'],
                $condition['single_price'],
                $condition['single_price']*$condition['quantity'],
                $condition['single_price']*$condition['quantity'],
                $condition['type'],
                User::active_user()->id,
                $condition['remark'],
                $condition['supplier_id'],
                $condition['enterprise_dominant']
            ))
            ->execute('newerp');
    }

    /**
     * （弃用）
     * 转存商品核对信息,并把订单状态改为4，的方法
     *   deliveryorders
     *   deliveryorders_bsk
     *   erp_orders
     * @param $erp_orders_id
     * @return $res boolean
     **/

    public static function seal_order_check_data($erp_orders_id){
            $deliveryorders = DB::select(array('*'))
                ->from('deliveryorders')
                ->where('order_id','=',$erp_orders_id)
                ->execute('goods_check')
                ->as_array();
            $deliveryorders_bsk = DB::select(array('*'))
                ->from('deliveryorders_bsk')
                ->where('order_id','=',$erp_orders_id)
                ->execute('goods_check')
                ->as_array();
            $erp_orders = DB::select(array('*'))
                ->from('erp_orders')
                ->where('erp_orders_id','=',$erp_orders_id)
                ->execute('goods_check')
                ->as_array();
            $erp_deliveryorders = DB::select(array('*'))
                ->from('erp_deliveryorders')
                ->where('order_id','=',$erp_orders_id)
                ->execute('readonlyerp');
        //查出来都有数据，就封存 且删除
            if($deliveryorders && $deliveryorders_bsk && $erp_orders && $erp_deliveryorders){
                $res = DB::insert('seal_data',array('erp_orders_id','table_name','data'))
                    ->values(array($erp_orders_id,'deliveryorders',json_encode($deliveryorders)))
                    ->values(array($erp_orders_id,'deliveryorders_bsk',json_encode($deliveryorders_bsk)))
                    ->values(array($erp_orders_id,'erp_orders',json_encode($erp_orders)))
                    ->execute('good_check');
                $erp_order_res = DB::insert('seal_data',array('erp_orders_id','table_name','data'))
                    ->values(array(array($erp_orders_id,'deliveryorders',json_encode($deliveryorders))))
                    ->execute('readonlyerp');
                if($res && $erp_order_res){
                    Database::instance('goods_check')->begin();
                    try{
                        DB::delete('deliveryorders')
                            ->where('order_id','=',$erp_orders_id)
                            ->execute('goods_check');
                        DB::delete('deliveryorders_bsk')
                            ->where('order_id','=',$erp_orders_id)
                            ->execute('goods_check');
                        DB::delete('erp_orders')
                            ->where('erp_orders_id','=',$erp_orders_id)
                            ->execute('goods_check');
                        Database::instance('goods_check')->commit();
                        $del_re = true;
                    }catch (Exception $e){
                        Database::instance('goods_check')->rollback();
                    }
                    $update_re = DB::update('erp_orders')
                                    ->set(array('orders_status' => 4))
                                    ->where('erp_orders_id' , '=' ,$erp_orders_id)
                                    ->execute('readonlyerp');
                }
            }
        if($res && $del_re && $update_re){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $erp_orders_id
     * @return object
     *
     */
    public static function change_erp_orders_status($erp_orders_id){
        $res =  DB::update('erp_orders')
            ->set(array('orders_status' => 4))
            ->where('erp_orders_id' , '=' ,$erp_orders_id)
            ->execute('readonlyerp');
        DB::insert('erp_ebay_log',array('erp_orders_id', 'cotent', 'writetime', 'writeer', 'types'))
            ->values(array(
                $erp_orders_id,
                '订单扫描回库，订单号是：'.$erp_orders_id,
                date('Y-m-d H:i:s'),
                User::active_user()->id,
                0,
            ))
            ->execute('readonlyerp');
        return $res;
    }


}
